package hlhj.fhp.supreme.activitys

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.liaoinstan.springview.container.DefaultHeader
import com.liaoinstan.springview.widget.SpringView
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import hlhj.fhp.supreme.BaseAty
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.javabean.CoverListBean
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.utils.JsonCallBack
import hlhj.fhp.supreme.utils.MyUtils
import kotlinx.android.synthetic.main.activity_cover_list_aty.*
import java.util.ArrayList

class CoverListAty : BaseAty(), SpringView.OnFreshListener {
    private lateinit var adp:BaseQuickAdapter<CoverListBean.DataBean,BaseViewHolder>
    private val datas=ArrayList<CoverListBean.DataBean>()
    override fun getContentId(): Int {
        return R.layout.activity_cover_list_aty
    }

    override fun initView() {
        setSp()
        adp=object :BaseQuickAdapter<CoverListBean.DataBean,BaseViewHolder>(R.layout.cover_list_item,datas){
            override fun convert(helper: BaseViewHolder?, item: CoverListBean.DataBean?) {
                Glide.with(mContext).load(item?.img).into(helper?.getView(R.id.itemIcon))
                helper?.setText(R.id.itemTittle,item?.title)
                if (item?.type==1){
                    helper?.setText(R.id.itemType,"类    型：视频")
                }else{
                    helper?.setText(R.id.itemType,"类    型：音频")
                }
                helper?.setText(R.id.itemPrice,"发布价格：￥${item?.money}")
                helper?.setText(R.id.itemTime,"自荐时间：￥${MyUtils.timeStampToStrYMD(item?.create_time!!.toLong())}")
                when(item?.status){
                    0->{
                        helper?.setText(R.id.itemStatus,"自荐状态： 审核中")
                    }
                    1->{
                        helper?.setText(R.id.itemStatus,"自荐状态： 审核通过")
                    }
                    2->{
                        helper?.setText(R.id.itemStatus,"自荐状态： 拒绝通过")
                    }
                }
            }
        }
        listView.adapter=adp
        listView.layoutManager=LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)

        OkGo.post<CoverListBean>(Urls.BaseUrl+Urls.COVER_LIST)
                .execute(object :JsonCallBack<CoverListBean>(this){
                    override fun onSuccess(response: Response<CoverListBean>?) {
                        val bean = response?.body()
                        if (bean?.code==1){
                            if (bean?.data!=null&&bean?.data.size>0){
                                datas.addAll(bean?.data)
                                adp.notifyDataSetChanged()
                            }
                        }
                    }
                })
    }

    private fun setSp() {
        spView.header=DefaultHeader(this)
        spView.setListener(this)
    }

    override fun initListener() {

        btExit.setOnClickListener {
            finish()
        }
        btSelf.setOnClickListener {
            val intent = Intent(this, CoverAty::class.java)
            startActivity(intent)
        }
    }

    override fun onLoadmore() {
    }

    override fun onRefresh() {
        datas.clear()
        OkGo.post<CoverListBean>(Urls.BaseUrl+Urls.COVER_LIST)
                .execute(object :JsonCallBack<CoverListBean>(this){
                    override fun onSuccess(response: Response<CoverListBean>?) {
                        spView.onFinishFreshAndLoad()
                        val bean = response?.body()
                        if (bean?.code==1){
                            if (bean?.data!=null&&bean?.data.size>0){
                                datas.addAll(bean?.data)
                                adp.notifyDataSetChanged()
                            }
                        }
                    }
                })
    }
}
